package com.lottery.controller.pc28.lobbyController;

import com.alibaba.fastjson.JSONObject;
import com.lottery.service.pc28.lobbyService.MyOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/api2/homepage")
public class CustomersController {

	@Autowired
	private MyOrderService myOrderService;

	// 日志
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 我的订单
	 */
	@CrossOrigin
	@RequestMapping(value = "/myOrder", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	@ResponseBody
	public String selectMyOrder(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		String body = "";
		try {
			body = readHttpBody(request);
			JSONObject obj = JSONObject.parseObject(body);
			logger.debug("myOrder in::,{}", body);
			int userId = obj.getInteger("userId");
			// String expect = obj.getString("expect");
			int pageSize = obj.getInteger("pageSize");
			int pageNow = obj.getInteger("pageNow");
			// type待处理
			int lotteyType = 0;
			// String selectMyOrderByUserId =
			// myOrderService.selectMyOrderByUserId(userId, expect, pageSize,
			// pageNow,
			// lotteyType);
			String selectMyOrderByUserId = myOrderService.selectMyOrderByUserId(userId, pageSize, pageNow, lotteyType);
			logger.debug("myOrder out::,{}", selectMyOrderByUserId);
			return selectMyOrderByUserId;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("*****selectMyOrder error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}

		return jObject.toJSONString();
	}

	/**
	 * 订单详情表
	 * 
	 * @param request
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "/orderDetails", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	@ResponseBody
	public String orderDetails(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		String body = "";
		try {
			body = readHttpBody(request);
			JSONObject obj = JSONObject.parseObject(body);
			int userId = obj.getInteger("userId");
			String expect = obj.getString("expect");
			int pageSize = obj.getInteger("pageSize");
			int pageNow = obj.getInteger("pageNow");
			return myOrderService.orderDetails(userId, expect, pageSize, pageNow);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("*****orderDetails error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}

		return jObject.toJSONString();
	}

	/**
	 * 解析json
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	private String readHttpBody(HttpServletRequest request) throws IOException {
		JSONObject jObject = new JSONObject();
		try {
			return (String) request.getAttribute("json");
		} catch (Exception e) {
			logger.error("*****readHttpBody error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}

		return jObject.toJSONString();
	}

}
